package com.woniu.woniuk16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.woniuk16.commens.ComUtil;

import com.woniu.woniuk16.dto.AccountDto;
import com.woniu.woniuk16.dto.AccountsDto;
import com.woniu.woniuk16.entity.WoniuAccount;
import com.woniu.woniuk16.entity.WoniuAccountRule;
import com.woniu.woniuk16.entity.WoniuRule;
import com.woniu.woniuk16.entity.WoniuUser;
import com.woniu.woniuk16.mapper.WoniuAccountMapper;
import com.woniu.woniuk16.param.EmailParam;
import com.woniu.woniuk16.param.FindAccountParam;
import com.woniu.woniuk16.service.WoniuAccountRuleService;
import com.woniu.woniuk16.service.WoniuAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.woniuk16.service.WoniuRuleService;
import com.woniu.woniuk16.service.WoniuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-16
 */
@Service
public class WoniuAccountServiceImpl extends ServiceImpl<WoniuAccountMapper, WoniuAccount> implements WoniuAccountService {
    @Autowired
    private WoniuRuleService woniuRuleService;
    @Autowired
    private WoniuAccountRuleService woniuAccountRuleService;
    @Autowired
    private WoniuUserService woniuUserService;

    @Override
    public AccountsDto getAccount(FindAccountParam findAccountParam) {




        QueryWrapper<WoniuAccount> qw = new QueryWrapper<>();
        if (findAccountParam.getKeyWord()!=null&&!findAccountParam.getKeyWord().trim().equals("")){
            qw.like("account_name",findAccountParam.getKeyWord());
        }

        qw.orderByDesc("account_id");
        Page<WoniuAccount> ipage = new Page<>();
        ipage.setSize(findAccountParam.getPageSize());
        ipage.setCurrent(findAccountParam.getPageIndex());

        Page<WoniuAccount> page1 = page(ipage, qw);


        List<WoniuAccount> records = page1.getRecords();

        AccountsDto accountsDto = new AccountsDto();
        List<AccountDto> accountDtos = ComUtil.copyList(records, AccountDto.class);
        List<Integer> userIds = records.stream().map(rd -> rd.getUserId()).collect(Collectors.toList());

        List<WoniuUser> woniuUsers = woniuUserService.listByIds(userIds);

        ComUtil.listAddByFields(accountDtos,woniuUsers,"userId",Integer.class);

        accountsDto.setAccountDtos(accountDtos);

        accountsDto.setPageIndex(page1.getCurrent());
        accountsDto.setTotal(page1.getSize());




        return accountsDto;
    }

    @Override
    public boolean changeEmail(EmailParam emailParam) throws AccountException {
        WoniuAccount byId = getById(emailParam.getAccountId());
        if (byId==null){
            throw new AccountException("无此账号");
        }
        byId.setAccountEmail(emailParam.getAccountEmail());
        saveOrUpdate(byId);
        return true;
    }


}
